﻿Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Web.Script.Serialization
Imports System.Web.Script.Services
Imports System.Runtime.Serialization


Namespace MyWebservice

    <ScriptService()> _
    <WebService(Namespace:="http://localhost")> _
    Public Class Folder
        Inherits System.Web.Services.WebService

        Dim DB As mDB = mDB.GetDB
        Dim CurrUser As mDB.User = MyApp.GetUser

        <WebMethod(EnableSession:=True)> _
        Public Function aaa(t As String) As String
            Return t
        End Function

        <WebMethod(EnableSession:=True)> _
        Public Function GetFolders(operation As String, id As Integer, tree_type As Integer) As List(Of TreeNode)
            Dim FolderList As New List(Of TreeNode)

            Dim Folders = CurrUser.Tab_Folder(tree_type, id).ToList
            If mDB.Folder.GetDataType(tree_type) = mDB.Folder.DataType.CATEGORIZE Then
                ' Folders.AddRange(DB.TAB.Folder.BY.Folder.User_Id(-1, tree_type, id).ToList)
            End If

            For Each f In Folders
                Dim n = New TreeNode
                n.attr.id = f.Ref_Id

                Select Case f.Folder_Parent_Id
                    Case 0

                        If f.User_Id = -1 Then
                            n.attr.rel = "drive"
                            n.data = "Sistema"
                        Else
                            n.attr.rel = "drive"
                            n.data = "Utente"
                        End If

                    Case Else
                        If CBool(f.Is_Shared) Then
                            n.attr.rel = "folder_share"
                        Else
                            n.attr.rel = "folder"

                        End If
                        n.data = f.Folder

                End Select


                Dim ChildCount = DB.TAB.Folder.BY.Folder.User_Id(f.User_Id, tree_type, f.Id).SetSelect("count(id)").ToObj(Of Integer)()

                If ChildCount > 0 Then
                    n.state = ("closed")
                Else
                    n.state = ("")
                End If
                FolderList.Add(n)

            Next


            Return FolderList

        End Function

        <WebMethod(EnableSession:=True)> _
        Public Function CreateFolder(operation As String, id As Integer, position As Integer, title As String, type As String, tree_type As Integer) As Result
            Dim res As New Result
            Dim e As New mDB.Folder With {
                .Folder = title,
                .Folder_Parent_Id = id,
                .User_Id = CurrUser.Id,
                .Folder_Type_Id = tree_type
            }

            e.InsertOnSubmit(DB)

            'DB.TAB.Folder.InsertOnSubmit(e)
            DB.SubmitChanges()

            'e.Ref_Id = e.Id
            'DB.SubmitChanges()

            res.status = 1
            res.id = e.Id  ' DB.GetLastID

            Return res

        End Function

        <WebMethod(EnableSession:=True)> _
        Public Function DndElaborations(id As Integer, type As String, tree_type As Integer) As Result
            Dim res As New Result

            Dim array() As String = type.Split({","}, StringSplitOptions.RemoveEmptyEntries)
            Dim t As mDB.Folder.DataType = tree_type

            For i = 0 To array.Length - 1

                Select Case t
                    Case mDB.Folder.DataType.QUERY
                        Dim e = CurrUser.View_Query.BY.Query.Id(array(i)).ToObj
                        e.Folder_Id = id

                        DB.SubmitChanges()

                    Case mDB.Folder.DataType.PROFILE
                        Dim e = CurrUser.Tab_Profile.BY.Profile.Id(array(i)).ToObj
                        e.Folder_Id = id

                        DB.SubmitChanges()

                    Case mDB.Folder.DataType.CATEGORIZE

                        Dim e = DB.TAB.Categorize.GetByPK(array(i))
                        Dim f = DB.TAB.Folder.GetByPK(e.Folder_Id)

                        If f.User_Id <> CurrUser.Id Then
                            e = e.Clone
                            DB.TAB.Categorize.InsertOnSubmit(e)
                        End If
                        e.Folder_Id = id

                        e.SubmitChanges()

                End Select


            Next


            res.status = 1
            Return res
        End Function


        '<WebMethod(EnableSession:=True)> _
        'Public Function DndElaborations(id As Integer, type As String, tree_type As Integer) As Result
        '    Dim CurrUser As mDB.User = MyApp.GetUser
        '    Dim res As New Result

        '    Dim array() As String = type.Split({","}, StringSplitOptions.RemoveEmptyEntries)
        '    Dim t As mDB.Folder.Type = tree_type

        '    For i = 0 To array.Length - 1

        '        Select Case mDB.Folder.GetDataType(t)
        '            Case mDB.Folder.DataType.QUERY
        '                Dim e = CurrUser.Tab_Query.BY.Query.Id(array(i)).ToObj
        '                e.Folder_Id = id

        '                DB.SubmitChanges()

        '            Case mDB.Folder.DataType.CATEGORIZE

        '                Dim e = DB.TAB.Categorize.GetByPK(array(i))
        '                Dim f = DB.TAB.Folder.GetByPK(e.Folder_Id)

        '                If f.User_Id <> CurrUser.Id Then
        '                    e = e.Clone
        '                    DB.TAB.Categorize.InsertOnSubmit(e)
        '                End If
        '                e.Folder_Id = id

        '                e.SubmitChanges()

        '        End Select


        '    Next


        '    res.status = 1
        '    Return res
        'End Function

        <WebMethod(EnableSession:=True)> _
        Public Function RenameFolder(operation As String, id As Integer, title As String, tree_type As Integer) As Result
            Dim res As New Result

            Dim e = CurrUser.Tab_Folder.BY.Folder.Ref_Id(id).ToObj
            e.Folder = title
            DB.SubmitChanges()

            res.status = 1
            Return res

        End Function

        <WebMethod(EnableSession:=True)> _
        Public Function RemoveFolder(operation As String, id As Integer, tree_type As Integer) As Result
            Dim res As New Result

            'controllo altre cartelle
            res.status = 1
            Dim f = CurrUser.Tab_Folder(tree_type, id).ToList
            If f.Count > 0 Then
                res.status = 0
            End If
            'controllo altre elab
            'Dim e = CurrUser.TabElaboration.ByFolderID(id).ToList
            'If e.Count > 0 Then
            '    res.status = 0
            'End If

            If res.status = 1 Then
                CurrUser.Tab_Folder.ByPK(id).Delete.ExecuteCommand()
                'CurrUser.ViewElaborationFolderSearch.ByPK(id).Delete.ExecuteCommand()
            End If

            Return res


        End Function

        <WebMethod(EnableSession:=True)> _
        Public Function MoveFolder(operation As String, id As Integer, ref As Integer, position As Integer, copy As Integer) As Result
            Dim res As New Result

            Dim f = CurrUser.Tab_Folder.BY.Folder.Ref_Id(id).ToObj
            f.Folder_Parent_Id = ref
            DB.SubmitChanges()

            res.status = 1
            Return res

        End Function

        Public Class Result
            Public status As Integer
            Public id As String
        End Class

        Public Class TreeNode
            Public attr As New NodeAttribute
            Public data As String
            Public state As String
        End Class

        Public Class NodeAttribute
            Public id As Integer
            Public rel As String


        End Class

    End Class

End Namespace
